/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.modules.cms.web.front;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.servlet.ValidateCodeServlet;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.modules.cms.entity.Article;
import cn.net.ecode.modules.cms.entity.ArticleData;
import cn.net.ecode.modules.cms.entity.Category;
import cn.net.ecode.modules.cms.entity.Comment;
import cn.net.ecode.modules.cms.entity.Link;
import cn.net.ecode.modules.cms.entity.Site;
import cn.net.ecode.modules.cms.service.ArticleService;
import cn.net.ecode.modules.cms.service.CategoryService;
import cn.net.ecode.modules.cms.service.CommentService;
import cn.net.ecode.modules.cms.service.LinkService;
import cn.net.ecode.modules.cms.utils.CmsUtils;

import com.google.common.collect.Lists;

/**
 * 网站Controller
 * @author ThinkGem
 * @version 2015-11-27
 */
@Controller
@RequestMapping(value = "${frontPath}")
public class FrontController extends BaseController{

	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ArticleService articleService;
	@Autowired
	private LinkService linkService;
	@Autowired
	private CommentService commentService;
	
	/**
	 * 主站首页
	 */
	@RequestMapping
	public String index(Model model) {
		Site site = CmsUtils.getSite(Site.MAIN_SITE_CODE);
		model.addAttribute("site", site);
		model.addAttribute("isIndex", true);
		return "modules/cms/front/themes/"+site.getTheme()+"/frontIndex";
	}
	
	/**
	 * 站点首页
	 */
	@RequestMapping(value = {"index-{siteCode}", "index-{siteCode}${urlSuffix}"})
	public String index(@PathVariable String siteCode, Model model) {
		if (Site.isMainSite(siteCode)){
			return "redirect:"+Global.getFrontPath();
		}
		
		// 获取站点信息
		Site site = CmsUtils.getSite(siteCode);
		model.addAttribute("site", site);
		
		// 子站有独立页面，则显示独立页面
		if (StringUtils.isNotBlank(site.getCustomIndexView())){
			model.addAttribute("isIndex", true);
			return "modules/cms/front/themes/"+site.getTheme()+"/"+site.getCustomIndexView();
		}
		
		// 否则显示子站第一个栏目
		List<Category> mainNavList = CmsUtils.getMainNavList(siteCode);
		if (mainNavList.size() > 0){
			String firstCategoryCode = CmsUtils.getMainNavList(siteCode).get(0).getId();
			return "redirect:"+Global.getFrontPath()+"/list-"+firstCategoryCode+Global.getUrlSuffix();
		}
		
		// 站点中无栏目，则显示栏目分类空白页
		else{
			return "modules/cms/front/themes/"+site.getTheme()+"/"+Category.DEFAULT_TEMPLATE+"Category";
		}
	}
	
	/**
	 * 内容列表
	 */
	@RequestMapping(value = {"list-{categoryCode}", "list-{categoryCode}${urlSuffix}"})
	public String list(@PathVariable String categoryCode, 
			@RequestParam(required=false, defaultValue="1") Integer pageNo,
			@RequestParam(required=false, defaultValue="30") Integer pageSize,
			Model model, HttpServletRequest request) {
		
		// 获取栏目信息
		Category category = CmsUtils.getCategory(categoryCode);
		if (category == null || !Category.STATUS_NORMAL.equals(category.getStatus())){
			Site site = CmsUtils.getSite(Site.MAIN_SITE_CODE);
			model.addAttribute("site", site);
			return "error/404";
		}
		
		// 获取站点信息
		Site site = CmsUtils.getSite(category.getSite().getId());
		model.addAttribute("site", site);
		
		// 当前栏目展现方式为：2：简介类栏目，栏目第一条内容
		if("2".equals(category.getShowModes())){
			return view(categoryCode, null, model, request);
		}
		
		// 当前展现方式为：0：默认，或者，1：栏目列表
		else{
			
			// 当前栏目的子栏目列表
			List<Category> categoryList = null;
			
			// 如果有子节点，则查询子栏目列表
			if (!category.getIsLeaf()){
				Category categoryParam = new Category();
				categoryParam.setSite(new Site(site.getSiteCode()));
				categoryParam.setParentCode(category.getId());
				categoryList = categoryService.findList(categoryParam);
				model.addAttribute("categoryList", categoryList);
			}else{
				categoryList = Lists.newArrayList();
			}
			
			// 当前栏目展现方式为：1 、无子栏目或公共模型，显示栏目内容列表；0：无子栏目或一个子栏目，显示栏目内容列表
			if("1".equals(category.getShowModes()) || categoryList.size() <= 1){
				
				// 有子栏目并展现方式为1，则获取第一个子栏目；无子栏目，则获取同级分类列表。
				if(categoryList.size()>0){
					category = categoryList.get(0);
				}

//				// 没有子栏目列表，则再获取一次当前级别的栏目列表
//				else{
////					if (category.getIsRoot()){
////						categoryList.add(category);
////					}else{
////						categoryList = categoryService.findListByParentCode(category.getParentCode(), category.getSite().getId());
//						categoryList = CmsUtils.getCategoryList(category.getSite().getSiteCode(), category.getParentCode(), -1, null);
////					}
//					model.addAttribute("categoryList", categoryList);
//				}
				
				// 如果第一个子栏目为简介类栏目，则获取该栏目第一篇文章并展现
				if ("2".equals(category.getShowModes())){
					return view(category.getCategoryCode(), null, model, request);
				}

				// 否则，获取内容列表信息
				else{
					// 文章模型
					if ("article".equals(category.getModule())){
						Page<Article> page = new Page<Article>(pageNo, pageSize);
						page = articleService.findPage(page, new Article(category), false);
						model.addAttribute("page", page);
					}
					// 链接模型
					else if ("link".equals(category.getModule())){
						Page<Link> page = new Page<Link>(pageNo, pageSize);
						//page = linkService.findPage(page, new Link(category), false);
						model.addAttribute("page", page);
					}
				}

				// 将数据信息传递到视图
				model.addAttribute("category", category);
	            CmsUtils.addViewConfigAttribute(model, category);
	            String view = Category.DEFAULT_TEMPLATE;
	        	if (StringUtils.isNotBlank(category.getCustomListView())){
	        		view = category.getCustomListView();
	        	}
				return "modules/cms/front/themes/"+site.getTheme()+"/"+view;
			}
			
			// 当前栏目展现方式为：0：默认时，有子栏目：显示子栏目列表
			else{
				model.addAttribute("category", category);
	            CmsUtils.addViewConfigAttribute(model, category);
	            String view = Category.DEFAULT_TEMPLATE+"Category";
	        	if (StringUtils.isNotBlank(category.getCustomListView())){
	        		view = category.getCustomListView();
	        	}
				return "modules/cms/front/themes/"+site.getTheme()+"/"+view;
			}
		}
	}

	/**
	 * 内容列表（通过url自定义视图）
	 */
	@RequestMapping(value = {"listc-{categoryCode}-{customView}", "listc-{categoryCode}-{customView}${urlSuffix}"})
	public String listCustom(@PathVariable String categoryCode, @PathVariable String customView, 
			@RequestParam(required=false, defaultValue="1") Integer pageNo,
			@RequestParam(required=false, defaultValue="30") Integer pageSize,
			Model model, HttpServletRequest request) {
		
		// 获取栏目信息
		Category category = CmsUtils.getCategory(categoryCode);
		if (category == null || !Category.STATUS_NORMAL.equals(category.getStatus())){
			Site site = CmsUtils.getSite(Site.MAIN_SITE_CODE);
			model.addAttribute("site", site);
			return "error/404";
		}
		
		// 获取站点信息
        Site site = CmsUtils.getSite(category.getSite().getId());
		model.addAttribute("site", site);

		// 将数据信息传递到视图
		model.addAttribute("category", category);
        CmsUtils.addViewConfigAttribute(model, category);
		return "modules/cms/front/themes/"+site.getTheme()+"/"+Category.DEFAULT_TEMPLATE+customView;
	}

	/**
	 * 显示内容
	 */
	@RequestMapping(value = {"view-{categoryCode}-{contentId}", "view-{categoryCode}-{contentId}${urlSuffix}"})
	public String view(@PathVariable String categoryCode, @PathVariable String contentId, Model model, HttpServletRequest request) {
		
		// 获取栏目信息
		Category category = CmsUtils.getCategory(categoryCode);
		if (category == null || !Category.STATUS_NORMAL.equals(category.getStatus())){
			Site site = CmsUtils.getSite(Site.MAIN_SITE_CODE);
			model.addAttribute("site", site);
			return "error/404";
		}
		
		// 文章模型
		if ("article".equals(category.getModule())){
			
//			// 获取当前级别的栏目列表
//			List<Category> categoryList = Lists.newArrayList();
////			if (category.getIsRoot()){
////				categoryList.add(category);
////			}else{
////				categoryList = categoryService.findListByParentCode(category.getParentCode(), category.getSite().getId());
//				categoryList = CmsUtils.getCategoryList(category.getSite().getSiteCode(), category.getParentCode(), -1, null);
////			}
//			model.addAttribute("categoryList", categoryList);
			
			// 获取文章
			Article article = null;
			// 设置内容ID，则获取文章内容
			if (StringUtils.isNotBlank(contentId)){
				article = articleService.get(new Article(contentId));
			}
			
			// 如果没有设置内容ID则获取栏目里的第一篇文章
			else{
				Page<Article> page = new Page<Article>(1, 1, -1);
				Article entity = new Article(category);
				page = articleService.findPage(page, entity, false);
				if (page.getList().size()>0){
					article = page.getList().get(0);
					article.setArticleData(articleService.get(new ArticleData(article.getId())));
				}
			}
			
			// 如果没有取到文章，则抛到404页面
			if (article == null || !Article.STATUS_NORMAL.equals(article.getStatus())){
				return "error/404";
			}
			
			// 文章阅读次数+1
			articleService.updateHitsAddOne(contentId);
			
			// 如果设置了外部链接，则跳转到指定链接
			if (StringUtils.isNotBlank(article.getHref())){
				if (article.getHref().startsWith(request.getContextPath())){
					article.setHref(article.getHref().replaceFirst(request.getContextPath(), ""));
				}
				return "redirect:"+article.getHref();
			}
			
			model.addAttribute("article", article);
			
			// 获取文章归属栏目的全信息
			article.setCategory(CmsUtils.getCategory(article.getCategory().getId()));
			model.addAttribute("category", article.getCategory());
			
			// 获取栏目所在站点全信息
            Site site = CmsUtils.getSite(category.getSite().getId());
            model.addAttribute("site", site);
			
            // 获取推荐文章列表
			List<Object[]> relationList = articleService.findByIds(article.getArticleData().getRelation());
			model.addAttribute("relationList", relationList);
			
			// 将数据信息传递到视图
            CmsUtils.addViewConfigAttribute(model, article.getCategory());
            CmsUtils.addViewConfigAttribute(model, article.getViewConfig());
            return "modules/cms/front/themes/"+site.getTheme()+"/"+CmsUtils.getArticleView(article);
		}
		
		// 链接模型
		else if ("link".equals(category.getModule())){
			/*Link link = linkService.get(contentId);
			if (link == null || !Link.STATUS_NORMAL.equals(link.getStatus())){
				return "error/404";
			}
			if (StringUtils.isNotBlank(link.getHref())){
				if (link.getHref().startsWith(request.getContextPath())){
					link.setHref(link.getHref().replaceFirst(request.getContextPath(), ""));
				}
				return "redirect:"+link.getHref();
			}*/
		}
		return "error/404";
	}
	
	/**
	 * 内容评论
	 */
	@RequestMapping(value = "comment", method=RequestMethod.GET)
	public String comment(Comment comment, String theme, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Comment> page = new Page<Comment>(request, response);
		Comment c = new Comment();
		c.setCategory(comment.getCategory());
		c.setContentId(comment.getContentId());
		c.setStatus(Comment.STATUS_NORMAL);
		page = commentService.findPage(page, c);
		model.addAttribute("page", page);
		model.addAttribute("comment", comment);
		return "modules/cms/front/themes/"+theme+"/frontComment";
	}
	
	/**
	 * 内容评论保存
	 */
	@ResponseBody
	@RequestMapping(value = "comment", method=RequestMethod.POST)
	public String commentSave(Comment comment, String validateCode,@RequestParam(required=false) String replyId, HttpServletRequest request) {
		if (StringUtils.isNotBlank(validateCode)){
			if (ValidateCodeServlet.validate(request, validateCode)){
				if (StringUtils.isNotBlank(replyId)){
					Comment replyComment = commentService.get(replyId);
					if (replyComment != null){
						comment.setContent("<div class=\"reply\">"+replyComment.getName()+":<br/>"
								+replyComment.getContent()+"</div>"+comment.getContent());
					}
				}
				comment.setIp(request.getRemoteAddr());
				comment.setCreateDate(new Date());
				Boolean isAudit = StringUtils.toBoolean(Global.getConfig("cms.comment.isAudit"));
				comment.setStatus(isAudit ? Comment.STATUS_AUDIT : Comment.STATUS_NORMAL);
				comment.setStatus(Comment.STATUS_AUDIT);
				commentService.save(comment);
				return "{result:1, message:'提交成功"+(isAudit?"，请等待审核":"")+"。'}";
			}else{
				return "{result:2, message:'验证码不正确。'}";
			}
		}else{
			return "{result:2, message:'验证码不能为空。'}";
		}
	}
	
	/**
	 * 站点地图
	 */
	@RequestMapping(value = {"map-{siteCode}", "map-{siteCode}${urlSuffix}"})
	public String map(@PathVariable String siteCode, Model model) {
		Site site = CmsUtils.getSite(siteCode);
		model.addAttribute("site", site);
		return "modules/cms/front/themes/"+site.getTheme()+"/frontMap";
	}
	
}
